from django.db.models import Count

from post.models import Post


def getRightInfo(request):
    # 1. 获取分类信息
    right_category = Post.objects.values('pcategory__cname', 'pcategory').annotate(c=Count('*')).order_by('-c')

    # 2. 实现近期文章
    right_recent = Post.objects.all().order_by('-create')[:3]

    # 3. 获取日期归档信息
    from django.db import connection
    cursor = connection.cursor()
    cursor.execute("select `create`, count('*') c FROM t_post GROUP BY DATE_FORMAT(`create`,'%Y-%m') ORDER BY c DESC,`create` DESC")
    right_file = cursor.fetchall()

    return {'right_category': right_category, 'right_recent': right_recent, 'right_file': right_file}